#define _CRT_SECURE_NO_WARNINGS 1
#include"book.h"

void Check_Capacity(Book* pc);

void LoadBook(Book* pc)
{
	//打开文件
	FILE* pf = fopen("book.txt", "rb");
	if (pf == NULL)
	{
		perror("LoadBook");
		return;
	}
	//读文件
	BookInfo tmp = { 0 };
	while (fread(&tmp, sizeof(BookInfo), 1, pf))
	{
		Check_Capacity(pc);
		pc->data[pc->sz] = tmp;
		pc->sz++;
	}
	//关闭文件
	fclose(pf);
	pf = NULL;
}

//初始化
void InitBook(Book* pc)
{
	assert(pc);//断言
	pc->sz = 0;
	pc->capacity = DEFAULT_SZ;
	pc->data = calloc(pc->capacity, sizeof(BookInfo));
	if (pc->data == NULL)
	{
		perror("InitBook->calloc");
		return;
	}
	//加载文件信息
	LoadBook(pc);
}
//增加容量
void Check_Capacity(Book* pc)
{
	if (pc->sz == pc->capacity)
	{
		BookInfo* ptr = (BookInfo*)realloc(pc->data, (pc->capacity + 2) * sizeof(BookInfo));
		if (ptr != NULL)
		{
			pc->data = ptr;
			pc->capacity += 2;
			printf("增容成功！\n");
		}
		else
		{
			perror("AddBook->realloc");
		}
	}
}

//增加图书
void AddBook(Book* pc)
{
	system("cls");
	assert(pc);//断言
	//增加容量
	Check_Capacity(pc);
	//增加信息
	printf("请输入名字:<");
	scanf("%s", pc->data[pc->sz].name);
	printf("请输入图书编码:<");
	scanf("%lld", &(pc->data[pc->sz].ISBN));
	printf("请输入作者:<");
	scanf("%s", pc->data[pc->sz].author);
	printf("请输入价格:<");
	scanf("%f", &(pc->data[pc->sz].price));
	printf("请输入库存:<");
	scanf("%d", &(pc->data[pc->sz].inventory));

	pc->sz++;
	printf("增加成功！\n");
	Sleep(600);
}

//查找图书
int Find_By_name(Book* pc, char name[])
{

}

//删除图书
void DelBook(Book* pc)
{
	system("cls");
	char name[NAME_MAX];
	assert(pc);
	if (pc->sz == 0)
	{
		printf("图书管理系统为空，无法删除！\n");
		goto Return;
	}
	printf("请输入想要删除书的名字:<");
	scanf("%s", name);
	//找到该本书的人
	int ret = Find_By_name(pc, name);
	if (ret == -2)
	{
		printf("该图书不存在\n");
		goto Return;
	}
	//删除
	int i = 0;
	for (i = 0; i < pc->sz - 1; i++)//防止越界
	{
		pc->data[i] = pc->data[i + 1];
	}
	pc->sz--;
	printf("删除成功！\n");
	Sleep(600);
	system("cls");
	return;
Return:
	{
		int j = 0;
		printf("输入-1返回:<");
		scanf("%d", &j);
		system("cls");
		return;
	}
}

//查找图书
void SearchBook(Book* pc)
{

}

//修改图书
void ModifyBook(Book* pc)
{

}

//显示图书
void ShowBook(const Book* pc)
{
	system("cls");
	assert(pc);
	if (pc->sz == 0)
	{
		int j = 0;
		printf("图书管理系统为空，无需打印！\n");
		printf("输入-1返回:<");
		scanf("%d", &j);
		system("cls");
		return;
	}
	int i = 0;
	printf("                             图书管理系统                         \n");
	for (i = 0; i < pc->sz; i++)
	{
		printf("名字：%-10s 编码：%-10lld 作者：%-5s 价格：%-12.2f 库存：%-6d\n",
			pc->data[i].name, pc->data[i].ISBN, pc->data[i].author, pc->data[i].price, pc->data[i].inventory);
	}
	printf("\n");
	int j = 0;
	printf("输入-1返回:<");
	scanf("%d", &j);
	if (j == 0)
		return;
	system("cls");
}
//通过名字排序
int sort_by_name(const void* e1, const void* e2)
{
	return strcmp(((BookInfo*)e1)->name, ((BookInfo*)e2)->name);
}
//通过编码排序
long long sort_by_ISBM(const void* e1, const void* e2)
{
	return (long long)((BookInfo*)e1)->ISBN - ((BookInfo*)e2)->ISBN;
}
//通过作者排序
int sort_by_author(const void* e1,const void* e2)
{
	return strcmp(((BookInfo*)e1)->author, ((BookInfo*)e2)->author);
}
//排序
void SortBook(Book* pc)
{
	int input = 0;
	system("cls");
	if (pc->sz == 0)
	{
		printf("图书为空，无法进行排序！\n");
		goto Return;
	}
	printf("请选择排序方式：\n");
	printf("1:通过书名排序\n");
	printf("2:通过编码排序\n");
	printf("3:通过作者排序\n");
	scanf("%d", &input);
	if (1 == input)
	{
		qsort(pc->data, pc->sz, sizeof(BookInfo), sort_by_name);
	}
	else if (2 == input)
	{
		qsort(pc->data, pc->sz, sizeof(BookInfo), sort_by_ISBM);
	}
	else if (3 == input)
	{
		qsort(pc->data, pc->sz, sizeof(BookInfo), sort_by_author);
	}
	printf("排序成功！\n");
	goto Return;
Return:
	{
		int j = 0;
		printf("输入-1返回:<");
		scanf("%d", &j);
		system("cls");
		return;
	}
}
//保存信息
void SaveBook(Book* pc)
{
	FILE* pf = fopen("book.txt", "wb");
	if (pf == NULL)
	{
		perror("SaveBook");
		return;
	}
	//写信息到文件
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		fwrite(pc->data + i, sizeof(BookInfo), 1, pf);
	}
	fclose(pf);
	pf = NULL;
}
//销毁
void DestroyBook(Book* pc)
{
	free(pc->data);
	pc->data = NULL;
	pc->sz = 0;
	pc->capacity = 0;
}